package cn.sw.common.entity;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 统一响应对象
 *
 * @param <T>
 */
@Data
@NoArgsConstructor
public class R<T> implements Serializable {

    private static final long serialVersionUID = 8823816280764405188L;

    public static final Integer SUCCESS = 200;
    public static final Integer ERROR = 500;

    private Integer code;
    private String msg;
    private T data;

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

//    成功的响应
    public static <T> R<T> ok(Integer code, String msg, T data) {
        return new R<T>(code, msg, data);
    }
    public static <T> R<T> ok(T data) {
        return new R<T>(SUCCESS, "请求成功", data);
    }
    public static <T> R<T> ok() {
        return ok(null);
    }

//    失败的响应
    public static <T> R<T> fail(Integer code, String msg, T data) {
        return new R<T>(code, msg, data);
    }
    public static <T> R<T> fail(T data) {
        return fail(ERROR,"请求失败", data);
    }
    public static <T> R<T> fail() {
        return fail(null);
    }
}
